package com.ksd.web.controller.tool;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

public class ZipFileFinderFilter {

    private static final FilenameFilter ZIP_FILTER = (dir, name) ->
            name.toLowerCase().endsWith(".zip");

    public static List<File> findZipFiles(File directory) {
        List<File> zipFiles = new ArrayList<>();
        if (!directory.exists() || !directory.isDirectory()) {
            return zipFiles;
        }

        // 查找当前目录下的ZIP文件
        File[] zipFilesInDir = directory.listFiles(ZIP_FILTER);
        if (zipFilesInDir != null) {
            for (File file : zipFilesInDir) {
                zipFiles.add(file);
            }
        }

        // 递归查找子目录
        File[] subDirs = directory.listFiles(File::isDirectory);
        if (subDirs != null) {
            for (File subDir : subDirs) {
                zipFiles.addAll(findZipFiles(subDir));
            }
        }

        return zipFiles;
    }

    public static void main(String[] args) {
        File rootDir = new File("E:\\浏览器下载");
        List<File> zipFiles = findZipFiles(rootDir);

        System.out.println("找到 " + zipFiles.size() + " 个ZIP文件：");
        for (File file : zipFiles) {
            System.out.println(file.getAbsolutePath());
        }
    }
}
